(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.test.sampleCompany.createController', createController);
	createController.$inject = [ '$scope', '$timeout', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'sampleCompanyService','commonService' ];
	function createController($scope, $timeout, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, sampleCompanyService, commonService) {
		var ctrl = this, companyInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.company = {
			subCompanyList : []
		};
		$scope.test = function() {
			console.log('scope test');
		};
		
		ctrl.linkClick = function(company, form) {
			console.log("clicked");
		};
		
		ctrl.siteCodeChanged = function(value) {
			//console.log(value);
		};
		
		ctrl.subCompanyCodeChanged = function(object, formCtrl) {
			//console.log(formCtrl);
		};

		ctrl.init = function() {
			var params = {
				companyCode : companyInfo.companyCode
			};
			ctrl.getHolidays = commonService.getHolidays;
			ctrl.dateDisabled = commonService.dateDisabled;
			ctrl.calendarCodes = ['O:US', 'O:AE112'];
			/*sampleCompanyService.getHolidays({calendarYear : '2017', calendarCodes : ['O:US', 'O:AE']}).then(function() {
				ctrl.dateDisabled = sampleCompanyService.dateDisabled;
			});*/
			//ctrl.innerGridOptions = ctrl.layoutConfig.formConfigs.subCompany.gridConfigs.subCompanyList.gridOptions;
			//ctrl.innerGridOptions.enableGridMenu = true;
			//ctrl.innerGridOptions.enableColumnMenus = true;
			//ctrl.innerGridOptions.selectionRowHeaderWidth = 30;
			//ctrl.innerGridOptions.allowCellFocus = false;
			//ctrl.innerGridOptions.enableRowSelection = false;
			//ctrl.innerGridOptions.enableRowHeaderSelection = false;
			
			//ctrl.innerGridOptions.enableOperation = true;
			//ctrl.innerGridOptions.operationRowHeaderWidth = 150;
			//ctrl.innerGridOptions.enableOperationRemoveSelected = true;
			ctrl.isRowSelectable = function(row) {
				return true;
			};
			ctrl.filterRowOperation = function (row, operation) {
				if (operation == 'add') {
					return true;
				} else {
					return false;
				}
			};
			function getCompanyCode(i) {
				switch (i) {
				case 0:
					return "AAA";
				case 1:
					return "BBB";
				default:
					return "CCC";
				}
			}
			ctrl.addRow = function(row){
				console.log('addRow:');
				row.companyCode = getCompanyCode(ctrl.company.subCompanyList.length % 3);
				row.companyType2 = ctrl.company.subCompanyList.length + 1;
				row.attrName = "attrName";
				row.attrValue = "attrValue";
				console.log(row);
			};
			ctrl.removeSelectedRows = function(){
				console.log('removeSelectedRows:');
			};
			ctrl.viewRow = function(row) {
				console.log('viewRow:');
				console.log(row);
			};
			ctrl.editRow = function(row){
				console.log('editRow:');
				console.log(row);
			};
			ctrl.addSubRow = function(row){
				console.log('addSubRow:');
				console.log(row);
			};
			ctrl.removeRow = function(row){
				console.log('removeRow:');
				console.log(row);
			};
			
			ctrl.siteCodeOptions = sampleCompanyService.getSiteCodeOptions();
			ctrl.companyTypeOptions = sampleCompanyService.getCompanyTypeOptions();
			ctrl.companyTypeOptions2 = sampleCompanyService.getCompanyTypeOptions2();
			ctrl.companyTypeLookupOptions = sampleCompanyService.getCompanyTypeLookupOptions();
			ctrl.sensitiveOptions = sampleCompanyService.getSensitiveOptions();
			ctrl.ccyOptions = sampleCompanyService.getCcyOptions();
			ctrl.calendarYear = 2017;
			authService.create(params).then(function(response) {
				ctrl.company = response.data || companyInfo;
				ctrl.company.subCompanyList = ctrl.company.subCompanyList || [];
			});
		};

		ctrl.createSave = function() {
			if (ctrl.company.taskId) {
				return authService.createContinueSave(ctrl.company).then(saveSuccessful, saveFailed);
			} else {
				return authService.createSave(ctrl.company).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.createSubmit = function() {
			if (ctrl.company.taskId) {
				return authService.createContinueSubmit(ctrl.company).then(submitSuccessful, submitFailed);
			} else {
				return authService.createSubmit(ctrl.company).then(submitSuccessful, submitFailed);
			}
		};

		ctrl.addSubCompany = function() {
			ctrl.company.subCompanyList.push({
				dataStatus : 'C'
			});
		};

		ctrl.removeSubCompany = function() {
			gridService.removeSelectedRows($scope);
		};

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedSubCompanyList = selectedRows;
		}

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.test.sampleCompany.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.company.list',
					'state' : 'cda.test.sampleCompany.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);